#version 130

uniform float point_size;
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D texture3;
uniform sampler2D texture4;
uniform sampler2D texture5;

uniform sampler2D blur;

in float vLife;
in vec4 Position;

out vec4 gl_FragColor;

const float UINT_MAX = 4294967295.0;
const vec4 startColor = vec4( 1.0f, 1.0f, 0.0f, 1.0f );
const vec4 endColor = vec4( 1.0f, 0.2f, 0.0f, 1.0f );
const float heightSmoke = 0.2;
const vec4 smokeColor = vec4( 0.6, 0.6, 0.6, 1.0f );

void main( void )
{
	vec4 color = texture2D(blur, gl_TexCoord[0].st);
	//vec2 texCoord = vec2( 0.0f, 0.0f);

	//gl_FragColor = texture2D(texture, texCoord);
	//gl_FragColor.a = vLife * 0.5;

	vec2 v_rot = normalize( Position.zw );
	vec4 texCoord = vec4( 0.0, 0.0, gl_PointCoord.xy );
	texCoord.zw	-= vec2( 0.5, 0.5 );

	texCoord.x	= texCoord.z * v_rot.x;
	texCoord.y	= texCoord.w * v_rot.x;
	texCoord.x	-= texCoord.w * v_rot.y;
	texCoord.y	+= texCoord.z * v_rot.y;

	float alpha = vLife;

	if ( vLife < 0.0f )
	{
		gl_FragColor = texture2D( texture5, texCoord.xy + vec2( 0.5, 0.5 ) );

		gl_FragColor *= smokeColor;

		alpha += 1.0f;
		alpha = alpha/100;
	}
	else
	{
		vec4 currentColor = startColor - ( ( startColor - endColor ) * ( 1 - vLife) );

		gl_FragColor = texture2D( texture4, texCoord.xy + vec2( 0.5, 0.5 ) );
		gl_FragColor *= currentColor;
	}

	if ( gl_FragColor.r < 0.3f && gl_FragColor.g < 0.3f && gl_FragColor.b < 0.3f )
		gl_FragColor.a = 0.0f;
	else
		gl_FragColor.a *= alpha;
}
